********************************************************************************
* Leipziger, L, Aarslew, L, & M. Christensen
* Replication Code for "Do Group-Based Inequalities Feel More Unjust?"
* POQ, 2026
* This code: Fig 4: Holding ethnicity constant
********************************************************************************
********************************************************************************
clear all
frame rename default data

* Open data:
use "data/US_data_clean.dta", clear

lab define country_lab 1 "India" 2 "\forceindent South Africa" 3 "\forceindent US"
lab values country country_lab

lab define treat 0 "Baseline" 1 "\forceindent Vertical ineq." 2 "\forceindent Intergroup ineq." 3 "\forceindent Within-black ineq." 4 "\forceindent Within-white ineq."
lab values D treat


* Fairness: 
qui reg fair_std i.D pretreat_fair if D != 1 & D!=0, rob					// Vertical group dropped (b/c we analyze the two "cleaned" versions [group fixed])
margins, dydx(D)
mat A = r(table)
est store fairness // for tables
estadd scalar ajr2 = e(r2_a)


* EFFORT: 
qui reg effort_std i.D pretreat_effort if D != 1 & D!=0, rob					// Vertical group dropped (b/c we analyze the two "cleaned" versions [group fixed])
margins, dydx(D)
mat B = r(table)
est store effort // for tables
estadd scalar ajr2 = e(r2_a)

* Anger: 
qui reg angry_std i.D if D != 1 & D!=0, rob
margins, dydx(D)
mat C = r(table)
est store angry // for tables
estadd scalar ajr2 = e(r2_a)

* Frustration: 
qui reg frustrated_std i.D if D != 1 & D!=0, rob
margins, dydx(D)
mat E = r(table)
est store frustrated // for tables
estadd scalar ajr2 = e(r2_a)

**making table SM7
esttab fairness effort angry frustrated using "output/tableSM7.tex", lab nogap wide tex cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3)))  star(+ 0.10 * 0.05 ** 0.01 *** 0.001) b(%9.3f) ///
	noobs nonotes nonumbers  ///
	stats(ajr2  N, labels("Adjusted R^2" "N") fmt(3 0)) varlabels(_cons Constant) ///
	refcat(3.D "\forceindent Intergroup ineq.")   ///
	mtitles("Fairness" "Effort" "Angry" ///
	"Frustration")  ///
	mgroups("Outcome", pattern(0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) replace
	

mat D = 1,0.9,A[1,2],A[5,2],A[6,2]\			///		
		2,1.1,A[1,3],A[5,3],A[6,3]\			///				
		1,1.9,B[1,2],B[5,2],B[6,2]\			///		
		2,2.1,B[1,3],B[5,3],B[6,3]\			///			
		1,2.9,C[1,2],C[5,2],C[6,2]\			///		
		2,3.1,C[1,3],C[5,3],C[6,3]\			///
		1,3.9,E[1,2],E[5,2],E[6,2]\			///		
		2,4.1,E[1,3],E[5,3],E[6,3]
		
	
		
		
frame create plot4
cwf plot4		
svmat D
rename D1 treatment						// 1 = Intergroup, 2 = Within black, 3 = within white
rename D2 Xpos							// Position on X-axis
rename D3 b								// Parameter estimate (predicted mean)
rename D4 ll							// lower confidence level
rename D5 ul							// upper confidence level


*Plot Figure 4
tw ///
	(rspike ll ul Xpos if treatment == 1, lc(black)) ///
	(rspike ll ul Xpos if treatment == 2, lc(black)) ///
	(scatter b Xpos if treatment == 1, msym(O) mc(black) msize(med)) ///
	(scatter b Xpos if treatment == 2, msym(O) mc(black) mfcolor(white) msize(med)) ///
	, yscale(range(-.5 .5) extend lc(none)) ytit("Effect Estimates (sd)", size(medsmall)) ///
	xscale(range(.2 4.9) extend) xlab(1 "Fairness" 2 "Effort" 3 "Anger" 4 "Frustration", labsize(small) notick gstyle(solid) glc(gs15%30)) xtit("Outcome", size(medsmall)) ///
	ylab(-.4 -.2 0 .2 .4, labsize(small) gstyle(solid) glc(gs15%30) notick) ///
	plotregion(lcolor(black) lwidth(medium)) ///
	legend(position(1) ring(0) ///
	order(3 "Interpersonal (Black)" 4 "Interpersonal (White)") row(3) size(vsmall)) ///
	plotregion(lcolor(black) lwidth(medium)) ///
	scale(1.3) graphregion(m(small)) ///
	yline(0, lcolor(gs8)) ///
	tit("") ///
	text(-.03 .15 "{it:(baseline = intergroup inequality)}", place(e) size(vsmall) color(gs8)) ///
	name(us_me, replace)
	
graph display, xsize(12) ysize(8) scale(1.3)
graph export "output/figure4.pdf", as(pdf) replace

		
		
	